import { defineStore } from 'pinia';
import { clearMenuStorage, clearRolesStorage, clearToken, getRolesStorage, setRolesStorage } from '@/utils/auth';
import { removeRouteListener } from '@/utils/route-listener';
import { UserState } from './types';
import useAppStore from '../app';
import { roleSystemList } from '@/api/role';

const useUserStore = defineStore('user', {
  state: (): UserState => ({
    name: undefined,
    avatar: undefined,
    job: undefined,
    organization: undefined,
    location: undefined,
    email: undefined,
    introduction: undefined,
    personalWebsite: undefined,
    jobName: undefined,
    organizationName: undefined,
    locationName: undefined,
    phone: undefined,
    registrationDate: undefined,
    accountId: undefined,
    certification: undefined,
    role: '',
  }),

  getters: {
    userInfo(state: UserState): UserState {
      return { ...state };
    },
  },

  actions: {
    // Set user's information
    setInfo(partial: Partial<UserState>) {
      this.$patch(partial);
    },

    // Reset user's information
    resetInfo() {
      this.$reset();
    },

    // Get user's information
    async info() {
      let rolesStorage = getRolesStorage()
      let ret: any
      if (!rolesStorage || rolesStorage.length < 1) {
        ret = await roleSystemList({})
        if (ret.code === 1) {
          setRolesStorage(JSON.stringify(ret.data))
        }
      }
      // const res = await getUserInfo();
      const role:any = window.localStorage.getItem('userRole') || 'admin';
      this.setInfo(
        {
          name: 'admin',
          avatar:
            '//lf1-xgcdn-tos.pstatp.com/obj/vcloud/vadmin/start.8e0e4855ee346a46ccff8ff3e24db27b.png',
          email: 'wangliqun@email.com',
          job: 'frontend',
          jobName: '前端艺术家',
          organization: 'Frontend',
          organizationName: '前端',
          location: 'beijing',
          locationName: '北京',
          introduction: '人潇洒，性温存',
          personalWebsite: 'https://www.arco.design',
          phone: '150****0000',
          registrationDate: '2013-05-10 12:10:00',
          accountId: '15012312300',
          certification: 1,
          role,
        }
      );
    },

    logoutCallBack() {
      const appStore = useAppStore();
      this.resetInfo();
      clearToken();
      clearRolesStorage()
      clearMenuStorage()
      removeRouteListener();
      appStore.clearServerMenu();
    },
    // Logout
    async logout() {
      this.logoutCallBack();
      // try {
      //   await userLogout();
      // } finally {
      //   this.logoutCallBack();
      // }
    },
  },
});

export default useUserStore;
